package fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.boltshow.LoginActivity;
import com.example.administrator.boltshow.R;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import adapter.MineFragmentAdapter;
import application.MyApplication;
import config.NetConfig;
import model.MineModel;

/**
 * Created by Administrator on 2016/8/29.
 */
public class MineFragment extends Fragment implements View.OnClickListener {
    private Button bt_login,bt_register;
    private RecyclerView mRecyclerView;
    private MineFragmentAdapter mAdapter;
    private List<MineModel.LinksBean> mData;
    private GridLayoutManager mManager;
    private ImageView ivShow;
    private TextView tvShow;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mine_fragment,null);

        initView(view);
        initData();
        setData();
        setListener();

        return view;
    }

    private void setListener() {
        bt_login.setOnClickListener(this);
        bt_register.setOnClickListener(this);
    }

    private void setData() {
        mManager = new GridLayoutManager(getActivity(),3, LinearLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(mManager);
        mRecyclerView.setAdapter(mAdapter);


    }

    private void initData() {
        mData = new ArrayList<>();
        mAdapter = new MineFragmentAdapter(mData,getActivity());
        x.http().get(new RequestParams(NetConfig.MINE_PATH), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                MineModel mineModel = gson.fromJson(result, MineModel.class);
                mData.addAll(mineModel.getLinks());
                mAdapter.notifyDataSetChanged();

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void initView(View view) {
        bt_login = (Button) view.findViewById(R.id.bt_mine_fragment_login);
        bt_register = (Button) view.findViewById(R.id.bt_mine_fragment_register);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.rv_mine_fragment);
        ivShow = (ImageView) view.findViewById(R.id.iv_mine_fragment_show);
        tvShow = (TextView) view.findViewById(R.id.tv_mine_fragment_show);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_mine_fragment_login :
                Intent intent1 = new Intent(getActivity(), LoginActivity.class);
                intent1.putExtra("flag",1);
                startActivity(intent1);
                break;
            case R.id.bt_mine_fragment_register :
                Intent intent2 = new Intent(getActivity(), LoginActivity.class);
                intent2.putExtra("flag",2);
                startActivity(intent2);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        MyApplication mApp = (MyApplication) getActivity().getApplication();
        if(mApp.getIsLogin()) {
            ivShow.setVisibility(View.VISIBLE);
            tvShow.setVisibility(View.VISIBLE);
            bt_login.setVisibility(View.GONE);
            bt_register.setVisibility(View.GONE);
            tvShow.setText(mApp.getUserName());
            if(!TextUtils.isEmpty(mApp.getUserIcon())) {
                x.image().bind(ivShow,mApp.getUserIcon(),mApp.getOptions());
            }

        }else {
            ivShow.setVisibility(View.GONE);
            tvShow.setVisibility(View.GONE);
            bt_login.setVisibility(View.VISIBLE);
            bt_register.setVisibility(View.VISIBLE);
        }
    }
}
